package cn.lyvust.eais.manage.controller;


import cn.lyvust.eais.common.annotation.SysLogs;
import cn.lyvust.eais.common.bean.ResponseCode;
import cn.lyvust.eais.common.bean.ResponseResult;
import cn.lyvust.eais.common.exception.RequestException;
import cn.lyvust.eais.common.util.BeansUtil;
import cn.lyvust.eais.manage.dto.param.rank.StudentAddParam;
import cn.lyvust.eais.manage.dto.param.rank.StudentParam;
import cn.lyvust.eais.manage.dto.param.rank.TeacherAddParam;
import cn.lyvust.eais.manage.dto.param.rank.TeacherParam;
import cn.lyvust.eais.manage.entity.Student;
import cn.lyvust.eais.manage.entity.Teacher;
import cn.lyvust.eais.manage.service.TeacherService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ggzheng
 * @since 2021-04-04
 */
@RequestMapping("/rank/teacher")
@RestController
@Api(tags = {"教师管理"})
public class TeacherController {
    @Autowired
    private TeacherService service;

    @ApiOperation("分页获取教师数据")
    @PostMapping("/list")
    public ResponseResult list(@RequestBody @Validated @ApiParam(value = "用户获取过滤条件") TeacherParam dto){
        QueryWrapper wrapper = new QueryWrapper();
        if (StringUtils.isNotEmpty(dto.getPhone())) {
            wrapper.eq("phone", dto.getPhone());
        }
        if (StringUtils.isNotEmpty(dto.getUserName())) {
            wrapper.eq("username", dto.getUserName());
        }
        if (StringUtils.isNotEmpty(dto.getNo())) {
            wrapper.eq("no", dto.getNo());
        }
        wrapper.orderByDesc("update_time");
        return ResponseResult.e(ResponseCode.OK,service.page(new Page<>(dto.getPage(), dto.getPageSize()), wrapper));
    }
    @ApiOperation("新增一个教师信息")
    @PostMapping("/add")
//    @RequiresPermissions("rank:teache:add")
    @SysLogs("添加一个教师数据")
    public ResponseResult add(@RequestBody @Validated @ApiParam(value = "用户获取过滤条件") TeacherAddParam dto){
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("phone", dto.getPhone());
        Teacher old = service.getOne(wrapper);
        if (old != null) {
            throw RequestException.fail("添加失败,教师已经存在!");
        }
        Teacher entity = new Teacher();
        BeanUtils.copyProperties(dto, entity);
        return ResponseResult.e(ResponseCode.OK, service.save(entity));
    }

    @ApiOperation("修改一个教师信息")
    @PostMapping("/update")
    //    @RequiresPermissions("rank:teache:update")
    @SysLogs("修改一个教师数据")
    public ResponseResult update(@RequestBody @Validated @ApiParam(value = "用户获取过滤条件") TeacherParam dto){
        QueryWrapper wrapper = new QueryWrapper();
        // 1.根据教师的姓名查询
        wrapper.eq("username", dto.getUserName());
        List resList = service.list(wrapper);
        if (resList == null || resList.size() == 0) {
            throw RequestException.fail("教师信息不存在!");
        }
        // 2.如果教师的姓名重复，说明有重名的老师，此时需要输入教师的手机号码
        if (resList.size() >= 2 && StringUtils.isEmpty(dto.getNo())) {
            throw RequestException.fail("教师姓名重复,请输入教师的教工编号!");
        }
        if (StringUtils.isNotEmpty(dto.getNo())) {
            wrapper.eq("no", dto.getNo());
        }
        resList = service.list(wrapper);
        if (resList.size() == 1) {
            Teacher entity = (Teacher) resList.get(0);
            BeansUtil.copyPropertiesIgnoreNull(dto, entity);
            service.updateById(entity);
        } else {
            throw RequestException.fail("修改失败,请联系管理员!");
        }
        return ResponseResult.e(ResponseCode.OK);
    }

    @ApiOperation("删除一个教师信息")
    @PostMapping("/delete")
    //    @RequiresPermissions("rank:teache:delete")
    @SysLogs("删除一个教师数据")
    public ResponseResult delete(@RequestBody @Validated @ApiParam(value = "用户获取过滤条件") TeacherParam dto){
        QueryWrapper wrapper = new QueryWrapper();
        // 1.根据教师的姓名查询
        wrapper.eq("username", dto.getUserName());
        List resList = service.list(wrapper);
        if (resList == null || resList.size() == 0) {
            throw RequestException.fail("教师信息不存在!");
        }
        // 2.如果教师的姓名重复，说明有重名的老师，此时需要输入教师的手机号码
        if (resList.size() >= 2 && StringUtils.isEmpty(dto.getPhone())) {
            throw RequestException.fail("教师姓名重复,请输入教师的手机号码!");
        }
        if (StringUtils.isNotEmpty(dto.getPhone())) {
            wrapper.eq("phone", dto.getPhone());
        }
        resList = service.list(wrapper);
        if (resList.size() == 1) {
            Teacher entity = (Teacher) resList.get(0);
            service.removeById(entity);
        } else {
            throw RequestException.fail("删除失败,请联系管理员!");
        }
        return ResponseResult.e(ResponseCode.OK);
    }
}
